package com.bytz.modules.cms.common.service.imp;

import com.bytz.common.exception.BytzBootException;
import com.bytz.common.system.api.ISysConfigApi;
import com.bytz.common.system.vo.SysConfigVo;
import com.bytz.modules.cms.common.constants.CmsConstant;
import com.bytz.modules.cms.common.model.CmsConfig;
import com.bytz.modules.cms.common.model.CmsSystemConfig;
import com.bytz.modules.cms.common.model.EChopConfig;
import com.bytz.modules.cms.common.service.CmsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CmsConfigServiceImpl implements CmsConfigService {

    @Autowired
    private ISysConfigApi configApi;

    @Override
    public CmsConfig getGlobal() {
        List<SysConfigVo> groupConfig = configApi.getByGroup(CmsConstant.ConfigKeys.GROUP_KEY);

        Map<String, Object> groupConfigMap = groupConfig.stream()
                .collect(Collectors.toMap(SysConfigVo::getConfigKey, SysConfigVo::getConfigValue));

        CmsConfig config = new CmsConfig();
        config.setTaxRate(getConfig(groupConfigMap, CmsConstant.ConfigKeys.TAX_RATE, BigDecimal.class));
        config.setSupplierUnitName(getConfig(groupConfigMap, CmsConstant.ConfigKeys.SUPPLIER_UNIT_NAME, String.class));
        config.setSupplierUnitAddress(getConfig(groupConfigMap, CmsConstant.ConfigKeys.SUPPLIER_UNIT_ADDRESS, String.class));
        config.setSupplierPhone(getConfig(groupConfigMap, CmsConstant.ConfigKeys.SUPPLIER_PHONE, String.class));
        config.setSupplierFax(getConfig(groupConfigMap, CmsConstant.ConfigKeys.SUPPLIER_FAX, String.class));
        config.setSupplierPostcode(getConfig(groupConfigMap, CmsConstant.ConfigKeys.SUPPLIER_POSTCODE, String.class));
        config.setSignedPlace(getConfig(groupConfigMap, CmsConstant.ConfigKeys.SIGNED_PLACE, String.class));
        config.setCurrency(getConfig(groupConfigMap, CmsConstant.ConfigKeys.CURRENCY, String.class));
        config.setDefaultPayType(getConfig(groupConfigMap, CmsConstant.ConfigKeys.DEFAULT_PAY_TYPE, String.class));
        config.setDefaultWarrantyPeriod(getConfig(groupConfigMap, CmsConstant.ConfigKeys.DEFAULT_WARRANTY_PERIOD, String.class));
        config.setPayTimeOut(getConfig(groupConfigMap, CmsConstant.ConfigKeys.PAY_TIME_OUT, Long.class));

        return config;
    }

    @Override
    public EChopConfig getEChopConfig() {
        List<SysConfigVo> groupConfig = configApi.getByGroup(CmsConstant.EChopConfigKeys.GROUP_KEY);

        Map<String, Object> groupConfigMap = groupConfig.stream()
                .collect(Collectors.toMap(SysConfigVo::getConfigKey, SysConfigVo::getConfigValue));

        EChopConfig config = new EChopConfig();
        config.setTemplateId(getConfig(groupConfigMap, CmsConstant.EChopConfigKeys.TEMPLATE_ID, Long.class));
        config.setCategoryId(getConfig(groupConfigMap, CmsConstant.EChopConfigKeys.CATEGORY_ID, Long.class));
        config.setSignatoryContract(getConfig(groupConfigMap, CmsConstant.EChopConfigKeys.SIGNATORY_CONTRACT, String.class));
        config.setSignatoryTenantName(getConfig(groupConfigMap, CmsConstant.EChopConfigKeys.SIGNATORY_TENANT_NAME, String.class));
        config.setSignatoryTenantType(getConfig(groupConfigMap, CmsConstant.EChopConfigKeys.SIGNATORY_TENANT_TYPE, String.class));
        config.setSignatorySerialNo(getConfig(groupConfigMap, CmsConstant.EChopConfigKeys.SIGNATORY_SERIAL_NO, Integer.class));
        config.setCreatorName(getConfig(groupConfigMap, CmsConstant.EChopConfigKeys.CREATOR_NAME, String.class));

        return config;
    }

    @Override
    public CmsSystemConfig getSystemConfig() {
        List<SysConfigVo> groupConfig = configApi.getByGroup(CmsConstant.SystemConfigKeys.GROUP_KEY);

        Map<String, Object> groupConfigMap = groupConfig.stream()
                .collect(Collectors.toMap(SysConfigVo::getConfigKey, SysConfigVo::getConfigValue));

        CmsSystemConfig config = new CmsSystemConfig();
        config.setOrderPrepayTimeLimit(getConfig(groupConfigMap, CmsConstant.SystemConfigKeys.ORDER_PREPAY_TIME_LIMIT, Integer.class));

        return config;
    }

    private <T> T getConfig(Map<String, Object> groupConfigMap, String key, Class<T> clazz) {
        Object result = groupConfigMap.get(key);
        if (result == null) {
            throw new BytzBootException("缺失系统参数【" + key + "】");
        }

        return clazz.cast(result);
    }
}
